package com.one.consumer;

import com.one.bean.*;
import com.one.constant.RabbitMQConstant;
import com.one.constant.RedisPrefixConstant;
import com.one.mapper.*;
import com.one.producer.RabbitMQTopicProducer;
import com.one.util.RedisUtils;
import com.one.vo.ProductParamVo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Create on 2022-01-17
 * <p>
 * Author: LiangWei
 * <p>
 * Description: 监听主题队列
 **/
@Slf4j
@Component
public class RabbitMQTopicConsumer1 {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private RabbitMQTopicProducer rabbitMQTopicProducer;
    @RabbitListener(queues = {RabbitMQConstant.RABBITMQ_TOPIC_QUEUE_ORDER_ONE})
    public void consumerTopicMsgFromTopicQueueOne(Map paramMap,Message message,Channel channel) throws IOException {
        //获取消息的唯一标识，消费者向rabbitMQ注册时，自动创建一个通道channel，rabbitmq向消费者，推送消息。每个消息都会自动携带一个唯一标识（仅限于当前通道递增的正整数）
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try{
            //1、获取消息队列中的值
           Integer userId = Integer.parseInt(String.valueOf(paramMap.get("userId")));
           ProductParamVo productParamVo = (ProductParamVo) paramMap.get("productParamVo");
           String tradeOrderNo = (String) paramMap.get("tradeOrderNo");

            //商品的实体
            Product product = productMapper.selectById(productParamVo.getProdId());
            BigDecimal singlePrice = product.getPrice();

            //因为是单个商品，所以不需要循环插入订单详情表，生成订单详情实体，准备通过消息队列存入数据库
            Orders order = new Orders();
            order.setOrderNo(tradeOrderNo);//插入这个商品的订单编号
            order.setTotalPrice(singlePrice);//插入商品单价
            order.setUserId(userId);//插入用户id
            order.setStatus(1);//将状态置为待支付1
            order.setTagColor(productParamVo.getColorId());//插入颜色
            order.setTagSize(productParamVo.getSizeId());//插入尺寸
            order.setProductId(productParamVo.getProdId());//插入商品id
            order.setCreateDate(new Date());//插入商品创建时间
            order.setProdNum(1);//插入数量

            //生成订单实体
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setOrderNo(tradeOrderNo);//插入订单编号
            orderInfo.setCreateDate(new Date());//插入创建时间
            orderInfo.setPostage(new BigDecimal("0"));//抢购包邮
            orderInfo.setStatus(1);//将状态置为待支付1

            //插入订单详情表
            orderMapper.insert(order);
            //插入订单表
            orderInfoMapper.insert(orderInfo);

            redisUtils.set(RedisPrefixConstant.ORDER_NO+String.valueOf(userId)+":"+tradeOrderNo,1,60);//置入redis中60s
            //发送死信队列绑定的直连队列
            Map directMap = new HashMap();
            directMap.put("OrderNo",tradeOrderNo);
            rabbitMQTopicProducer.sendDead(RabbitMQConstant.RABBITMQ_DIRECT_EXCHANGE,RabbitMQConstant.RABBITMQ_DIRECT_KEY,directMap);
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            channel.basicReject(deliveryTag,false);
        }
    }
}




